package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.product.entity.SpuImage;
import com.atguigu.gmall.product.entity.SpuInfo;
import com.atguigu.gmall.product.entity.SpuSaleAttr;
import com.atguigu.gmall.product.entity.SpuSaleAttrValue;
import com.atguigu.gmall.product.mapper.SpuInfoMapper;
import com.atguigu.gmall.product.service.SpuImageService;
import com.atguigu.gmall.product.service.SpuInfoService;
import com.atguigu.gmall.product.service.SpuSaleAttrService;
import com.atguigu.gmall.product.service.SpuSaleAttrValueService;
import com.atguigu.gmall.product.vo.SpuImageList;
import com.atguigu.gmall.product.vo.SpuSaleAttrList;
import com.atguigu.gmall.product.vo.SpuSaleAttrValueList;
import com.atguigu.gmall.product.vo.SpusaveInfoVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 *
 */
@Service
public class SpuInfoServiceImpl extends ServiceImpl<SpuInfoMapper, SpuInfo>
    implements SpuInfoService{

    @Autowired
    private SpuImageService spuImageService;

    @Autowired
    private SpuSaleAttrService spuSaleAttrService;

    @Autowired
    private SpuSaleAttrValueService spuSaleAttrValueService;

    /**
     * spu分页列表
     * @param page
     * @param limit
     * @param category3Id
     * @return
     */
    @Override
    public Page<SpuInfo> getSpuinfoPage(Long page, Long limit, Long category3Id) {
        Page<SpuInfo> spuInfoPage = new Page<>(page,limit);
        QueryWrapper<SpuInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("category3_id",category3Id);
        Page<SpuInfo> infoPage = this.page(spuInfoPage, wrapper);
        return infoPage;
    }

    /**
     * sup大保存
     * @param spusaveInfoVo
     */
    @Transactional
    @Override
    public void saveSpuInfo(SpusaveInfoVo spusaveInfoVo) {
        //1.保存supinfo详情
        SpuInfo spuInfo = new SpuInfo();
        BeanUtils.copyProperties(spusaveInfoVo,spuInfo);
        this.save(spuInfo);
        //2.保存spuImageList
        Long spuId = spuInfo.getId();
        List<SpuImageList> spuImageList = spusaveInfoVo.getSpuImageList();
        List<SpuImage> images = spuImageList.stream()
                .map(item -> {
                    SpuImage spuImage = new SpuImage();
                    //拷贝
                    BeanUtils.copyProperties(item, spuImage);
                    spuImage.setSpuId(spuId);
                    return spuImage;
                }).collect(Collectors.toList());
        spuImageService.saveBatch(images);
        //3.保存spuSaleAttrList销售属性列表
        List<SpuSaleAttrList> spuSaleAttrList = spusaveInfoVo.getSpuSaleAttrList();
        spuSaleAttrList.stream()
                .forEach(item ->{
                    //实现spusaleattr拷贝
                    SpuSaleAttr spuSaleAttr = new SpuSaleAttr();
                    BeanUtils.copyProperties(item,spuSaleAttr);
                    spuSaleAttr.setSpuId(spuId);
                    //保存spusaleattr
                    spuSaleAttrService.save(spuSaleAttr);
                    //拿到销售属性名称
                    String saleAttrName = spuSaleAttr.getSaleAttrName();
                    //拿到spuSaleAttrValueList列表
                    List<SpuSaleAttrValueList> spuSaleAttrValueList = item.getSpuSaleAttrValueList();
                    List<SpuSaleAttrValue> values = spuSaleAttrValueList.stream()
                            .map(value -> {
                                SpuSaleAttrValue spuSaleAttrValue = new SpuSaleAttrValue();
                                BeanUtils.copyProperties(value, spuSaleAttrValue);
                                spuSaleAttrValue.setSpuId(spuId);
                                spuSaleAttrValue.setSaleAttrName(saleAttrName);
                                return spuSaleAttrValue;
                            }).collect(Collectors.toList());
                    //保存
                    spuSaleAttrValueService.saveBatch(values);
                });

    }
}




